/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.sdk;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.RemoteException;
import android.os.UserManager;
import android.util.Log;

/**
 * FileName: EASLauncherControllerAPI.java <br>
 * Description : Launcher Controller API  <br>
 * Create Time: 2021/9/2 15:11 <br>
 * Update Time: 2021/9/2 15:11 <br>
 *
 * @since 1.0.0
 */
public class EASLauncherControllerAPI extends BaseAPI implements IEASLauncherControllerAPI {

    private static final String TAG = "EASLCAPI";

    private static EASLauncherControllerAPI instance;

    private final Context mContext;

    private IEASLCService iEASLCService;
    private EASBaseServiceConnection connection;

    private EASLauncherControllerAPI(Context context) {
        mContext = context;
    }

    public static EASLauncherControllerAPI get(Context context) {
        if (instance == null) {
            synchronized (EASLauncherControllerAPI.class) {
                if (instance == null) {
                    instance = new EASLauncherControllerAPI(context);
                }
            }
        }
        return instance;
    }


    /**
     * * Launcher Controller API 接口初始化
     * <p> 待回调返回 true 接口才能使用<br>
     *
     * @param callback {@link EASInitCallback} 初始化回调接口
     * @since 1.0.0
     */
    @Override
    public void init(final EASInitCallback callback) {
        if (callback == null || mContext == null) {
            throw new IllegalArgumentException("params is null");
        }
        UserManager mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        connection = new EASBaseServiceConnection(mContext, instance, callback);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !mUserManager.isUserUnlocked()) {
            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_USER_UNLOCKED);
            intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            mContext.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (connection != null && !connection.isBindSuccess()) {
                        connection.connectService();
                    }
                }
            }, intentFilter);
        } else {
            if (connection != null && !connection.isBindSuccess()) {
                connection.connectService();
            }
        }
    }

    /**
     * * 释放 Launcher Controller API 链路.
     *
     * @since 1.0.0
     */
    @Override
    public void release() {
        if (connection != null) {
            connection.release();
        }
    }

    @Override
    void setEASLCService(IEASLCService service) {
        iEASLCService = service;
    }

    @Override
    IEASLCService getEASLCService() {
        return iEASLCService;
    }

    private void logConnectError() {
        Log.e(TAG, "Please call initAPI method firstly and then call this after onInit(true)");
    }


    /**
     * * 发送消息.
     * <p> 消息发送接口 <br>
     *
     * @param notification {@link AbstractBaseNotification} 消息实体
     * @return 返回结果
     * @since 1.0.0
     */
    @Result
    @Override
    public int notify(AbstractBaseNotification notification) {
        if (notification == null) {
            throw new IllegalArgumentException("notification can not null");
        }

        if (iEASLCService == null) {
            logConnectError();
            return RESULT_FAIL;
        }

        try {
            Log.i(TAG,"notify a lc notification");
            return iEASLCService.notify(DataTransformUtil.transform(notification));
        } catch (RemoteException e) {
            Log.e(TAG,"notify a lc notification error " +e);
        }
        return RESULT_FAIL;
    }

    /**
     * * 取消消息.
     * <p> 取消消息接口 <br>
     *
     * @param notification {@link AbstractBaseNotification} 消息实体
     * @return {@link Result} 返回结果
     * @since 1.0.0
     */
    @Result
    @Override
    public int cancel(AbstractBaseNotification notification) {
        if (notification == null) {
            throw new IllegalArgumentException("notification can not null");
        }

        if (iEASLCService == null) {
            logConnectError();
            return RESULT_FAIL;
        }

        try {
            Log.i(TAG,"cancel a lc notification");
            return iEASLCService.cancel(DataTransformUtil.transform(notification));
        } catch (RemoteException e) {
            Log.e(TAG,"cancel a lc notification error " +e);
        }
        return RESULT_FAIL;
    }


}
